package com.zh.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.*;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;


/**
 * 扫描注解-->
 * <context:component-scan base-package="com.zh">
 * <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
 * </context:component-scan>
 * 引入properties文件-->
 * <context:property-placeholder location="classpath*:JDBC.properties" />
 */
@Configuration
@ComponentScan(value = "com.zh",
                excludeFilters = @ComponentScan.Filter(
                        type = FilterType.ANNOTATION,
                        classes = {Controller.class}
                ))
@PropertySource("classpath:JDBC.properties")
@EnableTransactionManagement
@Import({JdbcConfig.class, MybatisConfig.class})
public class SpringConfiguration {
/**
 *声明事务通知-->
 * <tx:annotation-driven transaction-manager="txManager"/>
 * 事务管理-->
 * <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
 * <property name="dataSource" ref="dataSource"></property>
 * </bean>
 */
    @Bean("transactionManager")
    public DataSourceTransactionManager dataSourceTransactionManager(@Autowired DataSource dataSource) {
        DataSourceTransactionManager dstm = new DataSourceTransactionManager();
        dstm.setDataSource(dataSource);
        return dstm;
    }
}
